﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MvcEngine.Core
{
    public class CommandRegistrator: ICommandRegistrator
    {
        private static IDictionary<Guid, List<ControlCommand>> commands = new Dictionary<Guid, List<ControlCommand>>();

        public static ICommandRegistrator Current 
        {
            get 
            {
                return IoC.Resolve<ICommandRegistrator>();
            }
        }

        public void Add(ControlCommand command)
        {
            if (commands.ContainsKey(command.ControlId))
            {
                commands[command.ControlId].Add(command);
            }
            else 
            {
                commands[command.ControlId] = new List<ControlCommand>();
                commands[command.ControlId].Add(command);
            }
        }

        public IList<ControlCommand> GetCommands(Guid controlId) 
        {
            if (commands.ContainsKey(controlId)) 
            {
                return commands[controlId];
            }
            return new List<ControlCommand>();
        }

        public ControlCommand GetCommand(Guid controlId, string name) 
        {
            if (commands.ContainsKey(controlId))
            {
                var controlCommands = commands[controlId];
                return controlCommands.SingleOrDefault(c => c.Name == name);
            }
            return null;
        }
    }
}
